package com.xiao.sulingwallet.service.impl;

import com.alipay.easysdk.factory.Factory;
import com.alipay.easysdk.kernel.util.ResponseChecker;
import com.alipay.easysdk.payment.page.models.AlipayTradePagePayResponse;
import com.xiao.sulingwallet.bindproperties.AlipayProperties;
import com.xiao.sulingwallet.service.AlipayService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;

/**
 * @author: 1322127758@qq.com
 * @date: 2022/3/14 17:40
 * @作用: 支付
 */
@Service("alipayService")
public class AlipayServiceImpl implements AlipayService {
    @Autowired
    private AlipayProperties alipayProperties;

    /**
     * 发起支付请求
     * @param subject :支付时显示的名
     * @param money   :支付的金额
     * @param orderSn :订单号
     * @return :返回支付表单
     */
    @Override
    public String getPayForm(String subject, BigDecimal money, String orderSn) throws Exception {

        /**
         * optional() 追加请求参数, timeout_express设置支付过期时间
         */
        AlipayTradePagePayResponse pay = Factory.Payment.Page().optional("timeout_express", "5m").pay(
                subject, orderSn, String.valueOf(money),
                alipayProperties.getReturnUrl());
        String payForm = "";
        if(ResponseChecker.success(pay)){
            //获取表单
            payForm = pay.getBody();
        }

        return payForm;
    }
}
